package entity;

import java.util.Date;


/**
 * An Event object with properties specified in RFC 5445.
 * ICalendar objects may contain multiple Event objects.
 * 
 * @author Sean Young
 * @version 1.0
 *
 */
public class Event implements EntityConstants{

	private Classification classification;
	private GeographicPosition geoPos;
	private Priority priority;
	private Summary summary;
	private DateTimeStart dtStart;
	private DateTimeEnd dtEnd;
	private RecurrenceRule rrule;
	private ExDate exDate;
	
	/**
     * Constructor for the Event object. Fields are defaulted
     * to null.
     */
	public Event() {
		this.classification = null;
		this.geoPos = null;
		this.priority = null;
		this.summary = null;
		this.dtStart = null;
		this.dtEnd = null;
		this.rrule = null;
		this.exDate = null;
	}
	
	/**
	 * Adds a CLASS property to the Event. The classification level 
	 * is defaulted to Public (1).
	 */
	public void addClass() {
		this.classification = new Classification();
	}
	
	/**
	 * Adds a CLASS property to the Event with the specified
	 * as the classification level.
	 * @param classification The classification level
	 *                        1 = PUBLIC, 2 = PRIVATE, 3 = CONFIDENTIAL.
     *                        As defined in EntityConstants.java.
	 */
	public void addClass(int classification) {
		this.classification = new Classification(classification);
	}
	
	/**
     * Adds a GEO property with the Event with the specified GPS 
     * coordinates as the geographic position.
     * @param latitude The latitude coordinates
     * @param longitude The longitude coordinates
     */
	public void addGeoPos(float latitude, float longitude) {
		this.geoPos = new GeographicPosition(latitude, longitude);
	}
	
	/**
	 * Adds a PRIORITY property to the Event with the specified 
	 * priority level.
	 * @param priority The level of priority
     *                 0 = undefined, 1 = Highest, 2 = Second Highest, etc.
	 */
	public void addPriority(int priority) {
		this.priority = new Priority(priority);
	}
	
	/**
	 * Adds a SUMMARY property to the Event with the specified
	 * string as the summary text.
	 * @param summary
	 */
	public void addSummary(String summary) {
		this.summary = new Summary(summary);
	}
	
	/**
	 * Adds a DTSTART property to the Event with the specified
	 * date as the start date of the event.
	 * @param startDate The start date of the event
	 */
	public void addDateTimeStart(Date startDate) {
		this.dtStart = new DateTimeStart();
		this.dtStart.setStartDate(startDate);
	}
	
	/**
	 * Adds a DTEND property to the Event with the specified date
	 * as the end date of the event.
	 * @param endDate The end date of the event
	 */
	public void addDateTimeEnd(Date endDate) {
		this.dtEnd = new DateTimeEnd();
		this.dtEnd.setEndDate(endDate);
	}
	
	/**
	 * Adds a RRULE to the Event. RRULE properties must be
	 * defined before adding the RecurrenceRule to the 
	 * Event.
	 * @param rrule The RecurrenceRule containing the specified
	 *              RRULE properties.
	 */
	public void addRecurrenceRule(RecurrenceRule rrule) {
		this.rrule = rrule;
	}
	
	public void addException(ExDate exDate){
		this.exDate = exDate;
	}
	
	
	/**
	 * Returns a string containing the values of the fields of the
	 * Event. Fields that were not added to the event will not be included. 
	 * 
	 * @return A string containing the properly formatted Event and its 
	 *         properties.
	 */
	public String toString() {
		StringBuilder eventString = new StringBuilder("BEGIN:VEVENT\r\n");
		
		if(this.dtStart != null)        eventString.append(this.dtStart.toString());
		if(this.dtEnd != null)          eventString.append(this.dtEnd.toString());
		if(this.rrule != null)          eventString.append(this.rrule.toString());
		if(this.exDate != null)					eventString.append(this.exDate.toString());
		if(this.classification != null) eventString.append(this.classification.toString());
		if(this.geoPos != null)         eventString.append(this.geoPos.toString());
		if(this.priority != null)       eventString.append(this.priority.toString());
		if(this.summary != null)        eventString.append(this.summary.toString());
		
		eventString.append("END:VEVENT\r\n");
		
		return eventString.toString();
	}

}
